<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/21
 * Time: 12:18
 */

namespace app\admin\controller;


use app\admin\model\Comment as CommentModel;
use think\Db;
use think\facade\Request;

class Comment extends Common
{
    /**
     * 评论列表
     * @return array|\think\response\View
     * @throws \think\exception\DbException
     */
    public function commentList()
    {
        if (Request::isAjax()) {
            $username = trim(input("username"));
            $status = trim(input("status"))?trim(input("status")):'';
            $title = trim(input("title"));
            $comment = trim(input("comment"));

            // where条件
            $where[] = ["b.username","like",'%'.$username.'%'];
            $where[] = ["c.title","like",'%'.$title.'%'];
            $where[] = ["b.comment","like",'%'.$comment.'%'];
            if($status != '' || $status != 0)
            {
                $where[] = ["b.status","=",$status];
            }
            $where[] = ["c.is_del","=",'1'];


            //获取分页参数
            $pageOption = $this->getPageAndPageSize(input('page'),input('limit'));

            //获取列表
            $field = "b.*,c.title";
            $list = CommentModel::getPageListByPageWithUser($pageOption['page'],$pageOption['pageSize'],$field,$where,['b.create_time'=>'desc']);
            // die(Db::name("comment")->getLastSql());
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return view("comment_list");
    }

    /**
     * 冻结评论
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function commentStatus()
    {
        if (Request::isAjax()) {
            $id = trim(input("id"));
            $status = trim(input("status"));

            //更新is_del
            $where[] = ["id","=",$id];
            $r = CommentModel::updateOneData($where,["update_time" => time(),'status'=>$status]);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"冻结成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"冻结失败","data"=>[]];
            }
        }
    }

    /**
     * 删除评论
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function commentDel()
    {
        if (Request::isAjax()) {
            $id = trim(input("id"));

            //更新is_del
            $r = CommentModel::softDel($id);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"删除成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"删除失败","data"=>[]];
            }
        }
    }
}